/**
 * copyright (c) by handpay 2012
 */
package com.handpay.zztong.hp.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.handpay.framework.utils.CommonUtils;
import com.handpay.framework.utils.DateUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.paymentbill.model.Bill;
import com.handpay.zztong.hp.paymentbill.model.Bill.APPCODE;
import com.handpay.zztong.hp.log.HPLog;

import java.util.List;

/**
 * @author：Klaus Liu E-mail: lpluck08@gmail.com
 * @date：2013-3-20 上午10:47:03
 */
public class QueryBillsAdapter extends BaseAdapter {


    // 交易账单列表
    private List<Bill> bills;

    private LayoutInflater inflater;
    private Activity mActivity;


    public QueryBillsAdapter(ZZTongActivity activity, List<Bill> bills) {
        this.bills = bills;
        mActivity = activity;
        inflater = LayoutInflater.from(activity);
    }

    @Override
    public int getCount() {
        return this.bills.size();
    }

    @Override
    public Object getItem(int position) {
        return this.bills.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BillPojo billPojo = new BillPojo();
        Bill bill = this.bills.get(position);
        if (convertView == null || convertView.getTag() == null) {
            convertView = (LinearLayout) inflater.inflate(R.layout.item_query_bill, null);
            billPojo.transTimeTextView = (TextView) convertView.findViewById(R.id.transTime);
            billPojo.transStatusTextView = (TextView) convertView.findViewById(R.id.transStatus);
            billPojo.transSNTextView = (TextView) convertView.findViewById(R.id.transSn);
            billPojo.cardNoInTextView = (TextView) convertView.findViewById(R.id.cardNoIn);
            billPojo.cardNoOutTextView = (TextView) convertView.findViewById(R.id.cardNoOut);
            billPojo.amountTextView = (TextView) convertView.findViewById(R.id.amount);
            billPojo.feeTextView = (TextView) convertView.findViewById(R.id.fee);
            billPojo.totalTextView = (TextView) convertView.findViewById(R.id.total);
            billPojo.hView = convertView.findViewById(R.id.trTransSn);
            billPojo.failureCausesLine = (TableRow) convertView.findViewById(R.id.failureCausesLine);
            billPojo.failureCausesTextView = (TextView) convertView.findViewById(R.id.failureCauses);
            billPojo.trRevokeSn = (TableRow) convertView.findViewById(R.id.trRevokeSn);
            billPojo.revokeSn = (TextView) convertView.findViewById(R.id.revokeSn);
            billPojo.transModeTestView = (TextView) convertView.findViewById(R.id.trTrans_mode);
            convertView.setTag(billPojo);
        } else {
            billPojo = (BillPojo) convertView.getTag();
        }
        HPLog.i(HPLog.TAG_RESULT,"billstatus:"+bill.getStatus());
        if (Bill.STATUS_SUCCESS == bill.getStatus()) {
            convertView.setBackgroundResource(R.drawable.edit_green_bg);
        } else if (Bill.STATUS_FAILED == bill.getStatus()) {
            convertView.setBackgroundResource(R.drawable.edit_red_bg);
            billPojo.failureCausesLine.setVisibility(View.VISIBLE);
        } else if (Bill.STATUS_PROCESSING == bill.getStatus()) {
            convertView.setBackgroundResource(R.drawable.edit_text_bg);
            billPojo.failureCausesLine.setVisibility(View.VISIBLE);
        }
        //撤销状态 处理
        billPojo.trRevokeSn.setVisibility(View.VISIBLE);
        if (bill.getRovokeState() == Bill.STATES_ROVOKE_SUCCESS) {
            billPojo.revokeSn.setText(R.string.revoke_success);
        } else if (bill.getRovokeState() == Bill.STATES_ROVOKE_ING) {
            billPojo.revokeSn.setText(R.string.revoke_ing);
        } else if (bill.getRovokeState() == Bill.STATES_ROVOKE_FAIL) {
            billPojo.revokeSn.setText(R.string.revoke_fail);
        } else {
            billPojo.trRevokeSn.setVisibility(View.GONE);
        }
        //交易方式
        if (bill.getTradeType() == Bill.TRAD_TYPE_NO) {
            billPojo.transModeTestView.setText(R.string.trade_unknoew);
        } else if (bill.getTradeType() == Bill.TRAD_TYPE_POSPAY) {
            billPojo.transModeTestView.setText(R.string.trade_pospay);
        } else if (bill.getTradeType() == Bill.TRAD_TYPE_ALIPAY||bill.getTradeType() == Bill.TRAD_TYPE_ALIPAY_SCAN) {
            billPojo.transModeTestView.setText(R.string.trade_alipay);
        } else if (bill.getTradeType() == Bill.TRAD_TYPE_WEIPAY||bill.getTradeType() == Bill.TRAD_TYPE_WEIPAY_SCAN) {
            billPojo.transModeTestView.setText(R.string.trade_weipay);
        } else if (bill.getTradeType() == Bill.TRAD_TYPE_NO_CARD_PAY) {
            billPojo.transModeTestView.setText(R.string.trade_no_card_pay);
        }
        //全渠道2.0
        /*else if(Bill.STATUS_SETTLED == bill.getStatus()){
            convertView.setBackgroundResource(R.drawable.edit_black_bg);
		}*/
        if (bill.getTransSN() != null && bill.getTransSN().length() > 0) {
            billPojo.transSNTextView.setText(bill.getTransSN());
        } else {
            billPojo.hView.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(bill.getTransTime())) {
            billPojo.transTimeTextView.setText(" ");
        } else {
            billPojo.transTimeTextView.setText(DateUtils.formatDateToString(DateUtils
                    .formatStringToDate(bill.getTransTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
        }
        if (Bill.STATUS_FAILED == bill.getStatus()) {
            billPojo.transStatusTextView.setText(R.string.deal_fail);
            billPojo.transStatusTextView.setTextColor(mActivity.getResources().getColor(R.color.red_color));
        } else if (Bill.STATUS_PROCESSING == bill.getStatus()) {
            billPojo.transStatusTextView.setText(R.string.trans_processing);
            billPojo.transStatusTextView.setTextColor(mActivity.getResources().getColor(R.color.light_blue));
        } else if (Bill.STATUS_SUCCESS == bill.getStatus()) {
            //全渠道2.0
            //} else if (Bill.STATUS_SUCCESS == bill.getStatus() || Bill.STATUS_SETTLED == bill.getStatus()) {
            billPojo.transStatusTextView.setText(R.string.deal_success);
            billPojo.transStatusTextView.setTextColor(mActivity.getResources().getColor(R.color.amount_color));
        } else if (Bill.STATUS_NOT_PAY == bill.getStatus()) {
            billPojo.transStatusTextView.setText(R.string.trans_not_pay);
            billPojo.transStatusTextView.setTextColor(mActivity.getResources().getColor(R.color.red_color));
        } else if (Bill.STATUS_CLOSED == bill.getStatus()) {
            billPojo.transStatusTextView.setText(R.string.trans_closed);
            billPojo.transStatusTextView.setTextColor(mActivity.getResources().getColor(R.color.red_color));
        }
        billPojo.cardNoInTextView.setText(bill.getCardNoIn());
        billPojo.cardNoOutTextView.setText(bill.getTransAccount());
        billPojo.amountTextView.setText(CommonUtils.formatMoney(bill.getAmount() / 100));
        billPojo.feeTextView.setText(CommonUtils.formatMoney(bill.getFee() / 100));
        billPojo.totalTextView.setText(CommonUtils.formatMoney(bill.getTotal() / 100));
        billPojo.failureCausesTextView.setText(bill.getTransStatus());
        if (bill.getAppcode() == APPCODE.TRANS) {
            billPojo.totalTextView.setText(CommonUtils.formatMoney(bill.getTotal() / 100));
            convertView.findViewById(R.id.cardNoInContainer).setVisibility(View.GONE);
            convertView.findViewById(R.id.amountContainer).setVisibility(View.GONE);
            convertView.findViewById(R.id.feeContainer).setVisibility(View.GONE);
            convertView.findViewById(R.id.failureCausesLine).setVisibility(View.GONE);
        }
        return convertView;
    }

    class BillPojo {
        public TextView transTimeTextView = null;
        public TextView transStatusTextView = null;
        public TextView transSNTextView = null;
        public TextView cardNoInTextView = null;
        public TextView cardNoOutTextView = null;
        public TextView amountTextView = null;
        public TextView feeTextView = null;
        public TextView totalTextView = null;
        public View hView = null;
        public TableRow failureCausesLine = null;
        public TextView failureCausesTextView = null;
        //撤销状态
        public TableRow trRevokeSn = null;
        public TextView revokeSn = null;
        //交易方式
        public TextView transModeTestView = null;
    }
}